package com.xy.blog.user.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.blog.core.dao.IBaseCommonDao;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.controller.model.req.query.OrganizationQuery;
import com.xy.blog.user.dao.IOrganizationDao;
import com.xy.blog.user.dao.mapper.OrganizationMapper;
import com.xy.blog.user.dao.model.OrganizationDO;
import com.xy.blog.user.exception.constants.OrganizationErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: 组织机构dao
 * @date 2020/3/4
 */
@Repository
public class OrganizationDaoImpl implements IOrganizationDao, IBaseCommonDao<OrganizationDO, OrganizationQuery> {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public PageResp<OrganizationDO> listByPage(PageReq<OrganizationQuery> req) {
        Page<Object> objects = PageHelper.startPage(req);
        List<OrganizationDO> organizationDOS = organizationMapper.selectAll();
        return PageResp.data(organizationDOS);
    }



    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(OrganizationDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            item.setCreateTime(LocalDateTime.now());
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(LocalDateTime.now());
        }
        if (item.getLeaf() == null) {
            item.setLeaf("true");
        }
        OrganizationDO query = new OrganizationDO();
        query.setCode(item.getCode());
        if (organizationMapper.selectCount(query) > 0) {
            throw new BaseException(OrganizationErrorCode.ORGANIZATION_EXITS);
        }
        organizationMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(OrganizationDO item) {
        organizationMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        organizationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public OrganizationDO getById(Long id) {
        return organizationMapper.selectByPrimaryKey(id);
    }

    @Override
    public OrganizationDO getByCode(String code) throws BaseException {
        OrganizationDO query = new OrganizationDO();
        query.setCode(code);
        return organizationMapper.selectOne(query);
    }

    @Override
    public List<OrganizationDO> select(OrganizationDO item) {
        Example example = new Example(OrganizationDO.class);
        Example.Criteria criteria = example.createCriteria();
        if ( StringUtils.isNotEmpty(item.getName())) {
            criteria.andLike("name", "%" + item.getName() + "%");
        }
        return organizationMapper.selectByExample(example);
    }

    @Override
    public int selectCount(OrganizationDO item) {
        return organizationMapper.selectCount(item);
    }
}
